package com.sxt.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sxt.common.CodeMsg;
import com.sxt.common.Constant;
import com.sxt.common.Result;
import com.sxt.domain.SysUser;
import com.sxt.form.SysUserForm;
import com.sxt.query.SysUserQuery;
import com.sxt.mapper.SysUserDao;
import com.sxt.service.SysUserService;
import com.sxt.vo.SysUserVO;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: Mr.xx
 * @date: 2021/3/18 19:51
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserDao sysUserDao;

    /**
     * 根据用户名和密码查询用户
     * @param username
     * @param password
     * @return
     */
    @Override
    public Result queryUser(String username, String password) {
        SysUser sysUser = sysUserDao.selectByNameAndPassword(username, password);
        if (sysUser == null) {
            return new Result(CodeMsg.USER_USER_PASSWORD_ERROR);
        }
        return new Result(sysUser);
    }

    /**
     * 用户的分页查询
     * @param query
     * @return
     */
    @Override
    public Result queryPage(SysUserQuery query) {
        // 开启分页查询
        Page<SysUserVO> sysUserVOPage = PageHelper.startPage(query.getPage(), query.getLimit());
        // 根据参数查询用户列表
        sysUserDao.selectList(query);
        return new Result(sysUserVOPage.toPageInfo());
    }

    /**
     * 新增用户
     *
     * @param sysUser
     * @return
     */
    @Override
    public Result add(SysUser sysUser) {
        // 进行业务数据校验
        SysUserQuery query = new SysUserQuery();
        // 校验用户名是否重复
        query.setLoginName(sysUser.getLoginName());
        // 根据登录名查询用户
        SysUserVO userVO = sysUserDao.selectUserByKeywords(query);
        if (userVO != null) {
            return new Result(CodeMsg.USER_LOGIN_NAME_EXIST_ERROR);
        }

        // 校验手机号是否重复
        query = new SysUserQuery();
        query.setPhone(sysUser.getPhone());
        // 根据手机号查询用户
        userVO = sysUserDao.selectUserByKeywords(query);
        if (userVO != null) {
            return new Result(CodeMsg.USER_PHONE_EXIST_ERROR);
        }

        // 校验身份证号是否重复
        query = new SysUserQuery();
        query.setIdCard(sysUser.getIdCard());
        // 根据身份证号查询用户
        userVO = sysUserDao.selectUserByKeywords(query);
        if (userVO != null) {
            return new Result(CodeMsg.USER_ID_CARD_EXIST_ERROR);
        }

        // 新增用户
        // 获取加密后的默认密码
        Md5Hash md5Hash = new Md5Hash(Constant.DEFAULT_PASSWORD, Constant.MD5_SALT, 2);
        sysUser.setLoginPassword(md5Hash.toString());
        sysUserDao.insert(sysUser);
        return new Result();
    }

    /**
     * 重置用户密码
     *
     * @param id
     * @return
     */
    @Override
    public Result resetPassword(Integer id) {
        Md5Hash md5Hash = new Md5Hash(Constant.DEFAULT_PASSWORD, Constant.MD5_SALT, 2);
        sysUserDao.updatePassword(id, md5Hash.toString());
        return new Result();
    }

    /**
     * 修改密码
     *
     * @param id
     * @param newPassword
     * @return
     */
    @Override
    public Result updatePassword(Integer id, String newPassword) {
        Md5Hash md5Hash = new Md5Hash(newPassword, Constant.MD5_SALT, 2);
        sysUserDao.updatePassword(id, md5Hash.toString());
        return new Result();
    }
}
